package top.tomxwd.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import top.tomxwd.common.Result;
import top.tomxwd.entity.Payment;

import java.nio.charset.Charset;

/**
 * @author xieweidu
 * @createDate 2020-07-08 23:07
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {

//    public static final String PAYMENT_URL = "http://localhost:8001";
    public static final String PAYMENT_URL = "http://PAYMENT-8001";

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/payment/create")
    public Result<Integer> create(@RequestBody Payment payment){
        HttpHeaders headers = new HttpHeaders();
        MimeType mimeType = MimeTypeUtils.parseMimeType("application/json");
        MediaType mediaType = new MediaType(mimeType.getType(), mimeType.getSubtype(), Charset.forName("UTF-8"));
        headers.setContentType(mediaType);
        HttpEntity<Payment> entity = new HttpEntity<>(payment, headers);
        ResponseEntity<Result<Integer>> responseEntity = restTemplate.exchange(PAYMENT_URL + "/payment/create", HttpMethod.POST, entity, new ParameterizedTypeReference<Result<Integer>>() {
        });
        return responseEntity.getBody();
    }

    @GetMapping("/payment/getById/{id}")
    public Result<Payment> create(@PathVariable("id") Long id){
//        HttpHeaders headers = new HttpHeaders();
//        MimeType mimeType = MimeTypeUtils.parseM`imeType("application/json");
//        MediaType mediaType = new MediaType(mimeType.getType(), mimeType.getSubtype(), Charset.forName("UTF-8"));
//        headers.setContentType(mediaType);
//        HttpEntity<Long> entity = new HttpEntity<>(id, headers);
        ResponseEntity<Result<Payment>> responseEntity = restTemplate.exchange(PAYMENT_URL + "/payment/getById/"+id, HttpMethod.GET, null, new ParameterizedTypeReference<Result<Payment>>() {
        });
        return responseEntity.getBody();
    }


}
